import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from 'vue-router';
import HomeView from '../views/HomeView.vue';
import {useUserStore} from '../stores/user.ts'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/Login',
      name:'Login',
      component: () => import('@/views/Login.vue'),
    },
    {
      path: '/Index',
      component: () => import('@/views/Index.vue'),
      children: [
       
        {
          path: '/memberUpdate',
          component: () => import('@/views/member/MemberUpdate.vue'),
        },
        {
          path: '/shopAdd',
          component: () => import('@/views/shop/ShopAdd.vue'),
        },
        {
          path: '/shopList',
          component: () => import('@/views/shop/ShopList.vue'),
        },
        {
          path: '/shopListNew',
          component: () => import('@/views/shop/ShopListNew.vue'),
        },
        {
          path: '/shopUpdate',
          component: () => import('@/views/shop/ShopUpdate.vue'),
        },
        {
          path: '/houseTypeAdd',
          component: () => import('@/views/house/HouseTypeAdd.vue'),
        },
        {
          path: '/houseTypeList',
          component: () => import('@/views/house/HouseTypeList.vue'),
        },
        {
          path: '/houseNoAdd',
          component: () => import('@/views/house/HouseNoAdd.vue'),
        },
        {
          path: '/houseNoList',
          component: () => import('@/views/house/HouseNoList.vue'),
        },
        {
          path: '/houseRowAdd',
          component: () => import('@/views/house/HouseRowAdd.vue'),
        },
        {
          path: '/housePlanAdd',
          component: () => import('@/views/house/HousePlanAdd.vue'),
        },
        {
          path: '/shopDetail',
          component: () => import('@/views/shop/ShopDetail.vue'),
        },
        {
          path: '/myInfo',
          component: () => import('@/views/my/MyInfo.vue'),
        },
        {
          path: '/houseState',
          component: () => import('@/views/house/HouseState.vue'),
        },
        {
          path: '/myTest',
          component: () => import('@/views/my/myTest.vue'),
        },
      ],
    },
    {
      path: '/',
      component: () => import('@/views/Demo.vue'),
    },
     
    {
      path: '/Home',  //路由地址
      name:'Home',  //路由名称
      component: () => import('@/views/Home.vue'),
      children:[
          {
            path: '/AddGoods',
            component: () => import('@/views/Goods/AddGoods.vue'),
          },
          {
            path: '/ShowGoods',
            component: () => import('@/views/Goods/ShowGoods.vue'),
          },
          {
            path: '/EditGoods/:gID',  //路由地址
            name:'EditGoods',  //路由名称
            component: () => import('@/views/Goods/EditGoods.vue'),
          },
          {
            path: '/AddRolePermission',  //路由地址
            name:'AddRolePermission',  //路由名称
            component: () => import('@/views/member/AddRolePermission.vue')
          },
          {
            path: '/EditMember/:mID',  //路由地址
            name:'EditMember',  //路由名称
            component: () => import('@/views/member/EditMember.vue')
          },
           {
            path: '/memberAdd',
            component: () => import('@/views/member/MemberAdd.vue'),
          },
          {
            path: '/memberList',
            component: () => import('@/views/member/MemberList.vue'),
          },
          {
            path: '/ShowOrder',
            component: () => import('@/views/Orders/ShowOrder.vue'),
          }
      ]
    }
  ],
});

//创建导航守卫 作用：在用户未登录时无法查看其它组件(验证pinia中是否存储了用户名称、登录状态)
//to: 即将要进入的目标路由  from：当前导航正要离开的路由  next:一定要调用，否则路由无法正常生效
router.beforeEach((to,from,next)=>{
  //创建仓库实例
  const userStore = useUserStore();

  //判断用户如果未登录且跳转的页面不是登录页面时，自动返回登录页面
  if(!userStore.isLogin&&to.name!=='Login')
  {
    //返回登录登陆页面,根据路由路径跳转时需加入 /
    next("/login");
  }
  else next();
  
})

export default router;
